//
//  PortListener.m
//  Mini Cidney
//
//  Created by Chris Karr on 1/13/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "PortListener.h"

#define PORT 31264
#define SERVICE @"_cidney._tcp."

@implementation PortListener

@synthesize connection;
@synthesize port;
@synthesize bonjour;
@synthesize name;
@synthesize number;
@synthesize imageData;

- (NSString *) machineName
{
	CFStringRef nameRef;
	NSString * computerName;
	nameRef = CSCopyMachineName ();
	computerName = [NSString stringWithString:(NSString *) nameRef];
	CFRelease (nameRef);

	return computerName;
}

- (void) awakeFromNib
{
	NSLog (@"awake");

	self.port = [[NSSocketPort alloc] initWithTCPPort:PORT];
    self.connection = [[NSConnection alloc] initWithReceivePort:port sendPort:nil];
    [self.connection setRootObject:self];
	
	self.bonjour = [[NSNetService alloc] initWithDomain:@"local" type:SERVICE name:[self machineName] port:PORT];
	[self.bonjour publish];
}

- (NSString *) incomingCaller:(NSString *) newName number:(NSString *) newNumber image:(NSData *) newImageData
{
	NSLog (@"caller: %@, number: %@", newName, newNumber);

	self.name = newName;
	self.number = newNumber;
	self.imageData = newImageData;
	
	return @"ok";
}

@end
